package com.itheima.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.*;
import com.itheima.bean.*;
import com.itheima.config.ThreadLocalConfig;
import com.itheima.dto.OrdersDto;
import com.sun.prism.impl.BaseContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Transactional
@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersDao ordersDao;

    @Autowired
    private AddressBookDao addressBookDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private OrderDetailDao orderDetailDao;
    @Autowired
    private  ShoppingCartDao shoppingCartDao;

    @Override
    public IPage<OrdersDto> findUserOrders(Params param) {
        Page<Orders> ordersPage = new Page<>(param.getPage(), param.getPageSize());
        LambdaQueryWrapper<Orders> qw1=new LambdaQueryWrapper<>();
        qw1.eq(Orders::getUserId, ThreadLocalConfig.get());
        qw1.orderByDesc(Orders::getOrderTime);
        IPage<Orders> ordersIpage = ordersDao.selectPage(ordersPage, qw1);
        List<OrdersDto> list1 = new ArrayList<>();
        IPage<OrdersDto> ordersDtoPage = new Page<>();
        for (Orders orders : ordersIpage.getRecords()) {
            LambdaQueryWrapper<OrderDetail> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(OrderDetail::getOrderId,orders.getId());
            List<OrderDetail> orderDetailList = orderDetailDao.selectList(qw2);
            OrdersDto ordersDto = new OrdersDto();
            ordersDto.setList(orderDetailList);
            BeanUtils.copyProperties(orders,ordersDto);
            list1.add(ordersDto);
        }
        return ordersDtoPage.setRecords(list1);
    }

    @Override
    public IPage<Orders> fingPage(int page, int pageSize, String number, Date beginTime, Date endTime) {


        Page<Orders> ordersPage = new Page<>(page,pageSize);

        LambdaQueryWrapper<Orders> qw1=new LambdaQueryWrapper<>();

        qw1.like((number!=null&&number!=""),Orders::getNumber,number);
        qw1.between(beginTime!=null&&endTime!=null,Orders::getOrderTime,beginTime,endTime);
        IPage<Orders> ordersPage1 = ordersDao.selectPage(ordersPage, qw1);
        return ordersPage1;
    }


    @Override
    public int add(Orders orders) {
        AddressBook addressBook = addressBookDao.selectById(orders.getAddressBookId());
        orders.setNumber(UUID.randomUUID().toString().replace("-",""));
        orders.setStatus(1);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setPhone(addressBook.getPhone());
        String addressName=(addressBook.getProvinceName()!=null?addressBook.getProvinceName():"")+
                (addressBook.getCityName()!=null?addressBook.getCityName():"")+
                (addressBook.getDistrictName()!=null?addressBook.getDistrictName():"")+
                (addressBook.getDetail()!=null?addressBook.getDetail():"");
        orders.setAddress(addressName);
        User user = userDao.selectById(orders.getUserId());
        orders.setUserName(user.getName());
        orders.setConsignee(addressBook.getConsignee());
        int row1 = ordersDao.insert(orders);

        LambdaQueryWrapper<ShoppingCart> sqw=new LambdaQueryWrapper<>();
        sqw.eq(ShoppingCart::getUserId,orders.getUserId());
        int row2=0;
      List<ShoppingCart> list = shoppingCartDao.selectList(sqw);
        for (ShoppingCart shoppingCart :list) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(shoppingCart.getName());
            orderDetail.setImage(shoppingCart.getImage());
            orderDetail.setOrderId(orders.getId());
            orderDetail.setNumber(shoppingCart.getNumber());
            orderDetail.setAmount(shoppingCart.getAmount());
            if (shoppingCart.getDishId()!=null){
                orderDetail.setDishId(shoppingCart.getDishId());
                if (shoppingCart.getDishFlavor()!=null){
                    orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
                }
            }else {
                orderDetail.setSetmealId(shoppingCart.getSetmealId());
            }
             row2 += orderDetailDao.insert(orderDetail);
        }


        return (row1>0&&row2==list.size())?1:0;
    }
}
